package com.kabuda.blog.controller;

import com.kabuda.blog.vo.params.PageParams;
import com.kabuda.blog.common.aop.LogAnnotation;
import com.kabuda.blog.service.ArticleService;
import com.kabuda.blog.vo.Result;
import com.kabuda.blog.vo.params.ArticleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author kabuda
 * @create 2023-01-16 14:49
 */
@Api(tags = "文章接口")
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    /**
     * 分页文章列表
     * @param pageParams
     */
    @ApiOperation("文章列表")
    @PostMapping
    @LogAnnotation(module = "文章",operator = "文章列表页面")
    public Result listArticles(@RequestBody PageParams pageParams){
        return articleService.listArticles(pageParams);
    }
    //@Cache(expire = 5 * 60 * 1000,name = "hot_article")
    //因为id过长会有精度损失，缓存注解设计不适合该处，所以不使用
    @ApiOperation("热点文章")
    @PostMapping("/hot")
    public Result hotArticles(){
        int limit=6;
        return articleService.hotsArticles(limit);
    }
    @ApiOperation("最新文章")
    @PostMapping("/new")
    public Result newArticles(){
        int limit=6;
        return articleService.newArticles(limit);
    }

    @ApiOperation("文章归档")
    @PostMapping("/listArchives")
    public Result listArchives(){
       return articleService.listArchives();
    }

    @ApiOperation("根据id查找文章信息")
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long id){
        return articleService.findArticleById(id);
    }

    @ApiOperation("发布文章")
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @ApiOperation("模糊查找文章标题")
    @PostMapping("/search")
    public Result search(@RequestBody Map<String,String> search){
        return articleService.search(search.get("search"));
    }

}
